from sklearn.feature_selection import VarianceThreshold
from scipy.stats import pearsonr
import pandas as pd
import matplotlib.pyplot as plt

def variance_demo():
    """
    特征降维——方差选择法
    :return: None
    """
    data = pd.read_csv("../resources/p01_machine_learning_sklearn/factor_returns.csv")
    # 1.实例化一个转换器类
    transfer = VarianceThreshold()
    # transfer = VarianceThreshold(threshold=5) #设置方差阈值
    # 2.调用fit_transform
    data_new = transfer.fit_transform(data.iloc[:, 1:-2])
    print("删除低方差特征的结果：\n", data_new)
    print("形状：\n", data_new.shape)
    return None


def pearsonr_demo():
    """
    特征降维——相关系数法
    :return: None
    """
    data = pd.read_csv("../resources/p01_machine_learning_sklearn/factor_returns.csv")
    # 1.计算皮尔逊相关系数
    r = pearsonr(data["revenue"], data["total_expense"])
    print("皮尔逊相关系数:", r)
    # 2.绘图
    plt.figure(figsize=(20, 8), dpi=100)
    plt.scatter(data["revenue"], data["total_expense"])
    plt.show()
    return None

if __name__ == "__main__":
    # 代码8：特征降维——方差选择法
    variance_demo()
    # 代码9：特征降维——方相关系数法
    pearsonr_demo()
